package com.qidian.study.servlets;

import com.qidian.study.pojo.Admin;
import com.qidian.study.service.AdminService;
import com.qidian.study.service.impl.AdminServiceImpl;
import com.qidian.study.util.DateFormat;
import com.qidian.study.util.JsonTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin")
public class AdminServlet extends BaseServlet {
    private AdminService adminService = new AdminServiceImpl();
    public void save(HttpServletRequest request,HttpServletResponse response)
            throws IOException,ServletException{
        // 添加管理员的操作
        // 获取请求参数
        String adminName = request.getParameter("adminName");
        String adminPass = request.getParameter("adminPass");
        int adminRole = 1;
        try{
            adminRole = new Integer(request.getParameter("adminRole"));
        }catch (NumberFormatException e){}
        //  将数据封装在一个Admin对象中
        Admin admin  = new Admin();
        admin.setAdminName(adminName);
        admin.setAdminPass(adminPass);
        admin.setAdminRole(adminRole);
        // 补全其他数据（这个补全的操作可以写在service中）
        admin.setStatus(1);
        admin.setCreateTime(DateFormat.getNowString());
        admin.setModifyTime(DateFormat.getNowString());
        // 调用service进行保存
        String result = JsonTools.object2json(adminService.save(admin));
        // 统一输出
        response.setContentType("application/json;charSet=utf-8");
        response.getWriter().write(result);
    }
}
